Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
We download the data from https://github.com/LuyiTian/sc_mixology/tree/master/data/csv
and copy it to the adenocarcinoma
folder (where the notebook is launched).
In [1]:
library(SingleCellExperiment)
library(scran)
library(scater)
library(Seurat)
In [2]:
run_qc <- function(sce) {
isSpike(sce, "ERCC") <- grepl("^ERCC", rownames(sce))
sce <- calculateQCMetrics(sce)
# Identify outliers, but without using the mouse as a batch
libsize.drop <- isOutlier(sce$total_counts, nmads=3, type="lower", log=TRUE)
feature.drop <- isOutlier(sce$total_features_by_counts, nmads=3, type="lower", log=TRUE)
spike.drop <- isOutlier(sce$pct_counts_ERCC, nmads=3, type="higher")
keep <- !(libsize.drop | feature.drop | spike.drop)
sce <- sce[,keep]
num.cells <- nexprs(sce, byrow=TRUE)
to.keep <- num.cells > 0
print(sum(!to.keep))
sce <- sce[to.keep,]
sce
}
In [3]:
tissue = 'sc_celseq2_5cl_p1'
path <- paste('adenocarcinoma', tissue, sep='/')
counts <- read.csv(paste(path, 'count', 'csv', sep='.'))
metadata <- read.csv(paste(path, 'metadata', 'csv', sep='.'))
sce_p1 <- SingleCellExperiment(assays = list(counts = as.matrix(counts)),
colData = as.data.frame(metadata))
tissue = 'sc_celseq2_5cl_p2'
path <- paste('adenocarcinoma', tissue, sep='/')
counts <- read.csv(paste(path, 'count', 'csv', sep='.'))
metadata <- read.csv(paste(path, 'metadata', 'csv', sep='.'))
sce_p2 <- SingleCellExperiment(assays = list(counts = as.matrix(counts)),
colData = as.data.frame(metadata))
tissue = 'sc_celseq2_5cl_p3'
path <- paste('adenocarcinoma', tissue, sep='/')
counts <- read.csv(paste(path, 'count', 'csv', sep='.'))
metadata <- read.csv(paste(path, 'metadata', 'csv', sep='.'))
sce_p3 <- SingleCellExperiment(assays = list(counts = as.matrix(counts)),
colData = as.data.frame(metadata))
universe <- intersect(intersect(rownames(sce_p1), rownames(sce_p2)), rownames(sce_p3))
sce_p1 <- sce_p1[universe,]
sce_p2 <- sce_p2[universe,]
sce_p3 <- sce_p3[universe,]
colnames(sce_p2) <- sub('p1', 'p2', colnames(sce_p2))
colnames(sce_p3) <- sub('p1', 'p3', colnames(sce_p3))
sce_celseq2_5cl <- cbind(sce_p1, sce_p2, sce_p3)
sce_celseq2_5cl <- run_qc(sce_celseq2_5cl)
colData(sce_celseq2_5cl)$label <- colData(sce_celseq2_5cl)$cell_line_demuxlet
saveRDS(sce_celseq2_5cl, 'adenocarcinoma/sce/sc_celseq2_5cl.rds')
name = 'sc_celseq2_5cl'
write.csv(as.matrix(counts(sce_celseq2_5cl)), paste(name, 'counts.csv', sep='.'))
write.csv(colData(sce_celseq2_5cl), paste(name, 'metadata.csv', sep='.'))
write.csv(rowData(sce_celseq2_5cl), paste(name, 'featuredata.csv', sep='.'))
In [4]:
tissue = 'sc_celseq2'
path <- paste('adenocarcinoma', tissue, sep='/')
counts <- read.csv(paste(path, 'count', 'csv', sep='.'))
metadata <- read.csv(paste(path, 'metadata', 'csv', sep='.'))
sce_celseq2 <- SingleCellExperiment(assays = list(counts = as.matrix(counts)),
colData = as.data.frame(metadata))
sce_celseq2$label <- sce_celseq2$cell_line_demuxlet
saveRDS(sce_celseq2, 'adenocarcinoma/sce/sc_celseq2.rds')
name = 'sc_celseq2'
write.csv(as.matrix(counts(sce_celseq2)), paste(name, 'counts.csv', sep='.'))
write.csv(colData(sce_celseq2), paste(name, 'metadata.csv', sep='.'))
write.csv(rowData(sce_celseq2), paste(name, 'featuredata.csv', sep='.'))
In [5]:
sce_celseq2_5cl
In [15]:
tissue = 'sc_10x'
path <- paste('adenocarcinoma', tissue, sep='/')
counts <- read.csv(paste(path, 'count', 'csv', sep='.'))
metadata <- read.csv(paste(path, 'metadata', 'csv', sep='.'))
sce_10x <- SingleCellExperiment(assays = list(counts = as.matrix(counts)),
colData = as.data.frame(metadata))
sce_10x$label <- sce_10x$cell_line_demuxlet
saveRDS(sce_10x, 'adenocarcinoma/sce/sc_10x.rds')
name = 'sc_10x'
write.csv(as.matrix(counts(sce_10x)), paste(name, 'counts.csv', sep='.'))
write.csv(colData(sce_10x), paste(name, 'metadata.csv', sep='.'))
write.csv(rowData(sce_10x), paste(name, 'featuredata.csv', sep='.'))
In [13]:
tissue = 'sc_10x_5cl'
path <- paste('adenocarcinoma', tissue, sep='/')
counts <- read.csv(paste(path, 'count', 'csv', sep='.'))
metadata <- read.csv(paste(path, 'metadata', 'csv', sep='.'))
sce_10x_5cl <- SingleCellExperiment(assays = list(counts = as.matrix(counts)),
colData = as.data.frame(metadata))
sce_10x_5cl$label <- sce_10x$cell_line_demuxlet
saveRDS(sce_10x, 'adenocarcinoma/sce/sc_10x_5cl.rds')
name = 'sc_10x_5cl'
write.csv(as.matrix(counts(sce_10x)), paste(name, 'counts.csv', sep='.'))
write.csv(colData(sce_10x), paste(name, 'metadata.csv', sep='.'))
write.csv(rowData(sce_10x), paste(name, 'featuredata.csv', sep='.'))
In [11]:
ratio <- function(df) {
table(df$label) / dim(df)[2]
}
In [21]:
ratio(sce_celseq2)
dim(sce_celseq2)[2]
ratio(sce_celseq2_5cl)
dim(sce_celseq2_5cl)[2]
In [23]:
ratio(sce_10x)
dim(sce_10x)[2]
ratio(sce_10x_5cl)
dim(sce_10x_5cl)[2]